package com.jgzx.utils;

import cn.hutool.core.util.StrUtil;
import com.jgzx.constant.CacheConstants;
import com.jgzx.constant.Constants;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jackesy
 * @className: AppContextUtil
 * @description: 应用上下文工具类
 * @create 2021/1/29 下午2:39
 **/
public class AppContextUtil {

    public static final String CLIENT_TYPE_PC = "PC";
    public static final String CLIENT_TYPE_APP = "app";
    public static final String CLIENT_TYPE_PLATFORM = "platform";

    /**
     * 获取请求token
     */
    public static String getToken() {
        String token = null;
        HttpServletRequest request = WebUtil.getRequest();
        if(request !=null){
            token = request.getHeader(Constants.HEADER_TOKEN);
        }
        return token;
    }

    /**
     * 获取客户端类型
     *
     * @return
     */
    public static String getClientType() {
        String token = getToken();
        if(StrUtil.isNotEmpty(token)){
            if (token.startsWith(CacheConstants.LOGIN_TOKEN_FLAG_PC_ENTERPRISE)) {
                return CLIENT_TYPE_PC;
            }
            if (token.startsWith(CacheConstants.LOGIN_TOKEN_FLAG_APP_ENTERPRISE)) {
                return CLIENT_TYPE_APP;
            }
        }
        return CLIENT_TYPE_PLATFORM;
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        HttpServletRequest request = WebUtil.getRequest();
        if(request !=null){
            String user_id = request.getHeader(Constants.HEADER_USER_ID);
            if (StrUtil.isNotEmpty(user_id)) {
                return Long.parseLong(user_id);
            }
        }
        return null;
    }

    /**
     * 获取企业ID
     */
    public static Long getEnterpriseId() {
        HttpServletRequest request = WebUtil.getRequest();
        if(request !=null){
            String enterprise_id = request.getHeader(Constants.HEADER_ENTERPRISE_ID);
            if (StrUtil.isNotEmpty(enterprise_id)) {
                return Long.parseLong(enterprise_id);
            }
        }
        return null;
    }

    /**
     * 获取功能标识
     */
    public static String getFunctionFlag() {
        HttpServletRequest request = WebUtil.getRequest();
        if(request != null){
            return request.getHeader(Constants.HEADER_FUNCTION_FLAG);
        }
        return null;
    }

}
